Conditions | 11 |
Total Lines | 43 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like bem.core.ts ➔ bem2arr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | const elementDelimiter = "__" |
||
19 | |||
20 | function bem2arr(query: BemAbsraction) { |
||
21 | const $return: string[] = [] |
||
22 | |||
23 | for (const block in query) { |
||
24 | const blockQ = query[block] |
||
25 | |||
26 | if (!blockQ) |
||
27 | continue |
||
28 | if (typeof blockQ !== "object") { |
||
29 | $return.push(block) |
||
30 | continue |
||
31 | } |
||
32 | |||
33 | for (const el in blockQ) { |
||
34 | const elementQ = blockQ[el] |
||
35 | , element = el === blockModKey ? block : `${block}${elementDelimiter}${el}` |
||
36 | |||
37 | if (!elementQ) { |
||
38 | el === blockModKey && $return.push(element) |
||
39 | continue |
||
40 | } |
||
41 | |||
42 | $return.push(element) |
||
43 | |||
44 | if (typeof elementQ !== "object") |
||
45 | continue |
||
46 | |||
47 | for (const mod in elementQ) { |
||
48 | const modValue: string|boolean = elementQ[mod] |
||
49 | if (!modValue) |
||
50 | continue |
||
51 | |||
52 | $return.push(`${element}${modDelimiter}${mod}${ |
||
53 | typeof modValue !== "string" |
||
54 | ? "" |
||
55 | : `${modDelimiter}${modValue}` |
||
56 | }`) |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | return $return |
||
62 | } |